// RedisWrapper.h
#pragma once
#include <QObject>
#include <QTimer>
#include <sw/redis++/redis++.h>
#include "TrTypes.h"
#include "TrSingleton.h"

namespace Market {

class RedisWrapper : public QObject, public TrSingleton<RedisWrapper> {
    Q_OBJECT
    friend class TrSingleton<RedisWrapper>;
public:
    // 初始化Redis连接
    bool init(const QString& configFile);
    
    // 发布数据
    void publishTick(const WTSTickStruct& tick);
    void publishBar(const WTSBarStruct& bar, const std::string& exchg, 
                   const std::string& code, TrPeriod period);
    
    // 获取Redis实例
    sw::redis::Redis* getRedis() { return m_redis.get(); }
    
    // 关闭连接
    void close();
    
private slots:
    // 打印统计信息
    void printStats();
    
private:
    explicit RedisWrapper(QObject* parent = nullptr);
    ~RedisWrapper();
    
    // 禁用拷贝和赋值
    RedisWrapper(const RedisWrapper&) = delete;
    RedisWrapper& operator=(const RedisWrapper&) = delete;
    
    // 连接Redis
    bool connect(const QString& host, uint16_t port, 
                const QString& password = QString(), 
                int db = 0);
    
    std::unique_ptr<sw::redis::Redis> m_redis;
    bool m_initialized{false};
    
    // Redis配置
    QString m_host;
    uint16_t m_port{6379};
    QString m_password;
    int m_db{0};
    
    // 发布通道
    QString m_tickChannel{"market.tick"};
    QString m_barChannel{"market.bar"};
    
    // 计数器
    int m_tickCount{0};          // 每分钟的tick计数
    uint64_t m_totalTicks{0};    // 总tick计数
    
    // 定时器
    QTimer m_timer;
};

#define REDIS_WRAPPER Market::RedisWrapper::instance()

} // namespace Market